package com.example.demo.config;

import org.activiti.engine.*;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.impl.persistence.StrongUuidGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class ActivitiConfig {
    public static final String path = "process/";
    @Autowired
    private DataSource dataSource;
    @Bean
    public StandaloneProcessEngineConfiguration standaloneProcessEngineConfiguration(){
        StandaloneProcessEngineConfiguration configuration = new StandaloneProcessEngineConfiguration();
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE);
        configuration.setIdGenerator(new StrongUuidGenerator());
        configuration.setAsyncExecutorActivate(false);
        configuration.setEventListeners(activitiEventListenerList());
        return configuration;
    }

    @Bean
    public ProcessEngine processEngine(@Autowired StandaloneProcessEngineConfiguration standaloneProcessEngineConfiguration) {
        RepositoryService repositoryService = standaloneProcessEngineConfiguration.getRepositoryService();
        RuntimeService runtimeService = standaloneProcessEngineConfiguration.getRuntimeService();
        TaskService taskService = standaloneProcessEngineConfiguration.getTaskService();
        return standaloneProcessEngineConfiguration.buildProcessEngine();
    }
    //在流程运行时对流程实例进行管理与控制
    @Bean
    public RuntimeService runtimeService(@Autowired ProcessEngine processEngine){
        return processEngine.getRuntimeService();
    }
    //提供一系列管理流程部署和流程定义的API
    @Bean
    public RepositoryService repositoryService(@Autowired ProcessEngine processEngine){
        return processEngine.getRepositoryService();
    }
    //对流程任务进行管理，例如任务提醒、任务完成和创建任务等
    @Bean
    public TaskService taskService(@Autowired ProcessEngine processEngine){
        return processEngine.getTaskService();
    }
    private List<ActivitiEventListener> activitiEventListenerList(){
        List<ActivitiEventListener> list = new ArrayList<ActivitiEventListener>();
        list.add(new MyActivitiEventListener());
        return list;
    }
    class MyActivitiEventListener implements ActivitiEventListener {

        @Override
        public void onEvent(ActivitiEvent event) {
            String name = event.getType().name();
            System.out.println("event name is : "+name);
            System.out.println("监听事件开始处理业务逻辑。。。");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("监听事件开始处理完业务逻辑！！！");
        }

        @Override
        public boolean isFailOnException() {
            return false;
        }
    }

}
